package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.CodeValues;
import org.kl.bf.entity.basic.DataDetail;
import org.kl.bf.repository.basic.DataDetailDao;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class DataDetailService {

	private DataDetailDao dataDetailDao;
	private CodeValuesService codeValuesService;

	public DataDetail getDataDetail(String guid) {
		return dataDetailDao.findOne(guid);
	}

	public void saveDataDetail(DataDetail entity) {
		dataDetailDao.save(entity);
	}

	public void deleteDataDetail(String guid) {
		dataDetailDao.delete(guid);
	}

	/**
	 * 删除多条记录
	 * 
	 * @param ids
	 */
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				dataDetailDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}

	public List<DataDetail> getAllDataDetail() {
		return dataDetailDao.findAll();
	}

	public List<DataDetail> getDataDetailListBydataTypeName(String codeName) {
		return dataDetailDao.findByDataTypeCodeName(codeName);
	}

	public List<CodeValues> getALLCodeName() {
		return dataDetailDao.findALLCodeValues();
	}

	public Page<DataDetail> getUserDataDetail(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<DataDetail> spec = buildSpecification(searchParams);
		Page<DataDetail> result = dataDetailDao.findAll(spec, pageRequest);
		return result;
	}

	public List<DataDetail> getAllDataDetail(Map<String, Object> searchParams, String sortType, String orderType) {
		Sort sort = CRUDUtils.buildSort(sortType, orderType);
		Specification<DataDetail> spec = buildSpecification(searchParams);
		List<DataDetail> result = dataDetailDao.findAll(spec, sort);
		if (result != null && result.size() != 0) {
			List<CodeValues> cvList = codeValuesService.findByTableCode(BasicConstants.TABLECODE_DATATYPE);
			if(cvList == null){
				return null;
			}
			for (CodeValues cv : cvList) {
				DataDetail dd = new DataDetail();
				dd.setId(cv.getId());
				dd.setDataName(cv.getCodeName());
				result.add(dd);
			}
		}
		return result;
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<DataDetail> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<DataDetail> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), DataDetail.class);
		return spec;
	}

	@Autowired
	public void setDataDetailDao(DataDetailDao dataDetailDao) {
		this.dataDetailDao = dataDetailDao;
	}

	@Autowired
	public void setCodeValuesService(CodeValuesService codeValuesService) {
		this.codeValuesService = codeValuesService;
	}

	public List<DataDetail> getAll(Map<String, Object> searchParams,String sortType, String orderType) {
		Specification<DataDetail> spec = KLDynamicSpecifications.buildSpecification(searchParams, DataDetail.class);
		return dataDetailDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}

}
